<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ReportOp;
use App\Admin\Extensions\ReportStatusOp;
use App\Http\Controllers\Controller;
use App\Model\User;
use App\Model\UserProfile;
use App\Model\UserReport;
use App\Util\ComUtil;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;

class ReportController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('举报记录');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(UserReport::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('记录ID');
            $grid->reportUser()->user_profile_id('举报人ID');
            $grid->reportUser()->nick_name('举报人昵称');
            $grid->images('举报截图')->display(function ($data) {
                return ComUtil::buidlImgaeHtml($data);
            });
            $grid->reason('举报原因');
            $grid->desc('举报详情');
            $grid->reportedUser()->user_profile_id('被举报人ID');
            $grid->reportedUser()->nick_name('被举报人昵称');
            $grid->reportedUser()->gender('被举报人性别')->display(function ($gender) {
                return $gender == 1 ? '男' : ($gender == 0 ? '女' : '-');
            });
            $grid->reportedUser()->role('被举报人角色')->display(function ($data) {
                return $data == UserProfile::ROLE_HOST ? '女用户' : '用户';
            });
            $grid->created_at('举报时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
            });
            $grid->reportedUserItem()->status('被举报人账户状态')->display(function ($data) {
                if (!$this->reported_user_id) return '';
                return User::STATUS[$data] ?? '未知状态';
            });
            $grid->remarks('备注')->editable();
            $grid->flag('处理状态')->display(function ($flag) {
                return $flag == 2 ? "已处理" : "未处理";
            });
            $grid->filter(function ($filter) {
                $filter->expand();
                $filter->disableIdFilter();
                $filter->column(1 / 2, function ($filter) {
                    $filter->equal('reportUser.user_profile_id', '举报用户ID');
                    $filter->between('created_at', '举报时间')->datetime();
                });
                $filter->column(1 / 2, function ($filter) {
                    $filter->equal('reportedUser.user_profile_id', '被举报用户ID');
                });
            });

            //禁用导出
            $grid->disableExport();
            //禁用行选择
            $grid->disableRowSelector();
            // $grid->disableActions();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->disableView();
                $actions->append(new ReportOp(
                    $actions->getKey(),
                    $actions->row,
                    'user/verify'
                ));
                $actions->append(new ReportStatusOp(
                     $actions->getKey(),
                     $actions->row,
                     'user_report/status'
                ));
            });
        });
    }

    protected function form($mode = '', $id = '')
    {
        return Admin::form(UserReport::class, function (Form $form) use ($mode, $id) {
            $form->text('remarks', '备注');
        });
    }

    function status()
    {
        $id = request('id');
        $flag = request('status');
        UserReport::where('id', $id)->limit(1)->update(['flag' => $flag]);
        return ['ret' => 0, 'message' => '操作成功！'];
    }
}
